package com.wex.cookbook.config.base;


import com.wex.cookbook.constant.enumPakge.ResultCodeEnum;

/**
 * 接口通用的返回对象。
 * @author : wex
 * @createTime : 2023/10/16 11:34
 */
public final class Result<T> {
    private  int code;

    private boolean success;
    private  String msg ;
    private  T data;

    public Result() {
    }

    public Result(int code, boolean success, String msg, T data) {
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = code == ResultCodeEnum.SUCCESS.getCode();
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.success = code == ResultCodeEnum.SUCCESS.getCode();
    }

    public Result(ResultCodeEnum resultCodeEnum) {
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMessage();
        this.success = code == ResultCodeEnum.SUCCESS.getCode();
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean getSuccess() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    //成功响应
    public static <T> Result<T> success(T data){
        return new Result<T>(ResultCodeEnum.SUCCESS.getCode(),true,ResultCodeEnum.SUCCESS.getMessage(),data);
    }

    //成功响应
    public static <T> Result<T> success(String msg,T data){
        return new Result<T>(ResultCodeEnum.SUCCESS.getCode(),msg,data);
    }

    //失败响应
    public static <T> Result<T> fail(String msg,T data){
        return new Result<T>(ResultCodeEnum.SYSTEM_EXCEPTION.getCode(),msg,data);
    }
    //失败响应
    public static <T> Result<T> fail(T data){
        return new Result<T>(ResultCodeEnum.SYSTEM_EXCEPTION.getCode(), false, ResultCodeEnum.SYSTEM_EXCEPTION.getMessage(),data);
    }

    public static Result success() {
        return success("");
    }


}
